package com.cnpc.rightslogan.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cnpc.rightslogan.entity.Player;
import com.cnpc.rightslogan.service.IPlayerService;
import com.cnpc.rightslogan.utils.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/13.
 */
@RestController
@RequestMapping("player")
@Api("PlayerController相关的api")
@Slf4j
public class PlayerController extends BaseController {

    @Resource
    private IPlayerService playerServiceImpl;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation(value = "获取玩家", notes = "获取玩家")
    @ResponseBody
    @GetMapping("/get/{roomNum}/{role}/{playerId}")
    public Player getPlayer(@PathVariable String roomNum, @PathVariable String role, @PathVariable Integer playerId) {
        log.info("获取玩家");
        //获取key
        String key = roomNum + role;
        Player player;

        if (stringRedisTemplate.opsForHash().hasKey(key, "playerId")) {
            Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(key);
            player = JSONObject.parseObject(JSON.toJSONString(entries), Player.class);
        } else {
            player = playerServiceImpl.getPlayer(playerId);
            putPlayerRedis(key, player);
        }
        return player;
    }

    @ApiOperation(value = "出牌", notes = "出牌")
    @ResponseBody
    @GetMapping("/attackList/discard/{cardTypes}/{roomNum}/{role}")
    public Player discard(@PathVariable String cardTypes, @PathVariable String roomNum, @PathVariable String role) {
        log.info("出牌");
        //获取key
        String key = roomNum + role;
        Player player;
        if (stringRedisTemplate.opsForHash().hasKey(key, "playerId")) {
            Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(key);
            player = JSONObject.parseObject(JSON.toJSONString(entries), Player.class);
            player = playerServiceImpl.underAttack(player, cardTypes);
            putPlayerRedis(key, player);
        } else {
            log.error("redis内存中丢失玩家");
            player = null;
        }
        return player;
    }

    /**
     * 把玩家信息放到redis中
     *
     * @param key    key
     * @param player value
     */
    public void putPlayerRedis(String key, Player player) {
//        Map map = JSON.parseObject(JSON.toJSONString(player),Map.class);
//        stringRedisTemplate.opsForHash().putAll(key,map);
        stringRedisTemplate.opsForHash().put(key, "playerId", player.getPlayerId().toString());
        stringRedisTemplate.opsForHash().put(key, "playerName", player.getPlayerName());
        stringRedisTemplate.opsForHash().put(key, "playerHp", player.getPlayerHp().toString());
        stringRedisTemplate.opsForHash().put(key, "playerMp", player.getPlayerMp().toString());
        stringRedisTemplate.opsForHash().put(key, "playerSp", player.getPlayerSp().toString());
    }
}